package com.bl.student.api.controller.chat;

import com.bl.student.api.controller.BaseController;
import com.bl.student.common.ApiResponseBody;
import com.bl.student.service.biz.chat.ChatService;
import com.bl.student.service.biz.vip.VipService;
import com.bl.student.service.vo.ListVo;
import com.bl.student.service.vo.chat.ChangeTeacherVo;
import com.bl.student.service.vo.chat.ChatTeacherVo;
import com.bl.student.service.vo.vip.IsVipVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * Created by yujingyi on 2018/3/9.
 */
@RestController
@Log4j2
@RequestMapping(value = {"/student/apis/chat"})
@Api(value = "我的话痨,聊天相关api", description = "我的话痨,聊天相关api")
public class ChatController extends BaseController {

    @Autowired
    VipService vipService;

    @Autowired
    ChatService chatService;

    @ApiOperation(value = "判断是否VIP接口", notes = "isVip: true 是VIP false 不是VIP")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "mobile", required = true, paramType = "query", dataType = "string", defaultValue = ""),
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "query", dataType = "string", defaultValue = ""),
            @ApiImplicitParam(name = "sid", value = "sid", required = true, paramType = "query", dataType = "string", defaultValue = "")})
    @GetMapping(value = {"/is_vip"})
    public ApiResponseBody<IsVipVo> isVip(
            @RequestParam(value = "mobile", required = true) String mobile,
            @RequestParam(value = "token", required = true) String token,
            @RequestParam(value = "sid", required = true) String sid,
            HttpServletRequest request) {

        IsVipVo isVipVo = vipService.isVip(getUserInfo(request));

        return ApiResponseBody.createSuccessBody(isVipVo);
    }

    @ApiOperation(value = "获取老师接口", notes = "获取老师接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "mobile", required = true, paramType = "query", dataType = "string", defaultValue = ""),
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "query", dataType = "string", defaultValue = ""),
            @ApiImplicitParam(name = "sid", value = "sid", required = true, paramType = "query", dataType = "string", defaultValue = ""),
            @ApiImplicitParam(name = "page", value = "页数", required = false, paramType = "query", dataType = "int", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页大小", required = false, paramType = "query", dataType = "int", defaultValue = "10")})
    @GetMapping(value = {"/teacher_list"})
    public ApiResponseBody<ListVo<ChatTeacherVo>> teacherList(
            @RequestParam(value = "mobile", required = true) String mobile,
            @RequestParam(value = "token", required = true) String token,
            @RequestParam(value = "sid", required = true) String sid,
            @RequestParam(value = "page", required = false, defaultValue = "1") int page,
            @RequestParam(value = "size", required = false, defaultValue = "10") int size,
            HttpServletRequest request) {

        Pageable pageable = new PageRequest(page - 1, size);

        ListVo<ChatTeacherVo> list = chatService.getTeacherList(getUserInfo(request), pageable);

        return ApiResponseBody.createSuccessBody(list);
    }

    @ApiOperation(value = "报名老师接口", notes = "报名老师接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "mobile", required = true, paramType = "query", dataType = "string", defaultValue = ""),
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "query", dataType = "string", defaultValue = ""),
            @ApiImplicitParam(name = "sid", value = "sid", required = true, paramType = "query", dataType = "string", defaultValue = ""),
            @ApiImplicitParam(name = "teacherAccount", value = "teacherAccount", required = true, paramType = "query", dataType = "string", defaultValue = "")})
    @PostMapping(value = {"/enroll"})
    public ApiResponseBody enroll(
            @RequestParam(value = "mobile", required = true) String mobile,
            @RequestParam(value = "token", required = true) String token,
            @RequestParam(value = "sid", required = true) String sid,
            @RequestParam(value = "teacherAccount", required = true) String teacherAccount,
            HttpServletRequest request) {

        chatService.enroll(getUserInfo(request), teacherAccount);
        return ApiResponseBody.createSuccessBody(null);
    }

    @ApiOperation(value = "更换老师申请", notes = "更换老师申请接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "mobile", required = true, paramType = "query", dataType = "string", defaultValue = ""),
            @ApiImplicitParam(name = "token", value = "token", required = true, paramType = "query", dataType = "string", defaultValue = ""),
            @ApiImplicitParam(name = "sid", value = "sid", required = true, paramType = "query", dataType = "string", defaultValue = "")})
    @PostMapping(value = {"/change"})
    public ApiResponseBody change(
            @RequestParam(value = "mobile", required = true) String mobile,
            @RequestParam(value = "token", required = true) String token,
            @RequestParam(value = "sid", required = true) String sid,
            @Valid @RequestBody ChangeTeacherVo body, BindingResult result,
            HttpServletRequest request) {
        ApiResponseBody validResult = validParams(result);
        if (validResult != null) {
            return validResult;
        }
        chatService.change(getUserInfo(request), body);
        return ApiResponseBody.createSuccessBody(null);
    }

}
